﻿using InstaMe.Controls;
using InstaMe.Source.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InstaMe.Source.ViewModels
{
    public class DiaryItemViewModel : INotifyPropertyChanged
    {
        public DiaryItemViewModel()
        {
            _items = new CustomObservableCollection<DiaryItem>();
        }

        public void LoadDiaryItems(List<DiaryItem> listItems, bool isAppend = false)
        {
            if (!isAppend)
            {
                _items.ManualClear();
            }

            for (int i = 0; i < listItems.Count; i++)
            {
                _items.ManualAdd(listItems[i]);
            }
        }

        public void CreateFakeData()
        {
            Random a = new Random();
            for (int i = 0; i < 10; i++)
            {
                _items.ManualAdd(new DiaryItem(i, DateTime.Now.AddMinutes(i), "Text-Nguyen Van Toan"+i.ToString(), "Photo-" + i.ToString(), "Video-" + i.ToString(), "Audio-" + i.ToString(), DiaryItem.TYPE_TEXT));
            }
        }

        private CustomObservableCollection<DiaryItem> _items;
        public CustomObservableCollection<DiaryItem> Items
        {
            get { return _items; }
            set
            {
                _items = value;
                NotifyPropertyChanged("Items");
            }
        }

        public void DeleteFeed(long aDiaryItemId)
        {
            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i].getId() == aDiaryItemId)
                {
                    Items.ManualRemove(Items[i]);
                    break;
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
